package com.bdqn.springweb;

import jakarta.annotation.Resource;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

@SpringBootTest
public class UserControllerTester {
    @Resource
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    @Before("")
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testGetUser() throws Exception {
        String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2")).andReturn().getResponse().getContentAsString();
        System.out.println("mvcResult===" + mvcResult);
    }

    @Test
    public void testAddUser() throws Exception {
        final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("username", "RESTful");
        params.add("password", "123456");
        params.add("userRoleId", "2");
        params.add("userFlag", "1");
        mockMvc.perform(MockMvcRequestBuilders.post("/user").params(params)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());
    }

}
